# -*- coding: utf-8 -*-

import  wx
import  wx.aui
import  os
import  sys

from  perspective import main as pmain
from  perspective import nav as nav
from  perspective import output as poutput

#import logtextctrl as logtc    
from menubar import menu as menuclass    
from menubar import toolbar

assertMode = wx.PYAPP_ASSERT_DIALOG

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"代码机器", pos=(20,20), #size=(200,100),
                        style=wx.DEFAULT_FRAME_STYLE);
        

class RunApp(wx.App):
    def __init__(self,redirect): #redirect the log info
        wx.App.__init__(self, redirect=redirect)
        #print "init..."
    
    def getMgr(self):
        return self._mgr
    
    def OnInit(self):
        wx.Log_SetActiveTarget(wx.LogStderr())
        #self.log = logtc.LogTextCtrl()
        self.SetAssertMode(assertMode)
        
        #frame = wx.Frame(None, -1, u"代码机器", pos=(20,20),) #size=(200,100),  style=wx.DEFAULT_FRAME_STYLE)
        
        
        
        frame = MyFrame()
        self.myframe = frame #MyFrame()
        
        self._mgr = wx.aui.AuiManager(frame)        
        
        frame.CreateStatusBar()
        
        
        
        menu = menuclass.Menu(self)
        menuBar = menu.getMenuBar()

        ns = {}
        ns['wx'] = wx
        ns['app'] = self
        ns['frame'] = frame
        
        frame.SetMenuBar(menuBar)
        frame.Show(True)
        
        #frame.Bind(wx.EVT_CLOSE, self.OnCloseFrame)

        #win = runTest(frame, frame, Log())

        tbc = toolbar.ToolBar(self)
        tb = tbc.getToolBar()
        
        """
        self.win = msgpanel.MsgPanel(frame)

        self.Bind(wx.EVT_SIZE, self.OnSize)

        # a window will be returned if the demo does not create
        # its own top-level window
        if self.win:
            # so set the frame to a good size for showing stuff
            frame.SetSize((640, 480))
            self.win.SetFocus()
            self.window = self.win
            ns['win'] = self.win
            frect = frame.GetRect()

        else:
            # It was probably a dialog or something that is already
            # gone, so we're done.
            frame.Destroy()
            return True
        
        """
        frame.SetSize((800, 600))
        self.SetTopWindow(frame)
        
        self.frame = frame
        

        # todo: aui manager cause double event


        # create several text controls
        
        self.p1 = nav.NavPanel(self.frame,wx.Size(300,600))
        self.p2 = poutput.OutputPanel(self.frame,wx.Size(600,200))
        
        p3 = pmain.MainPanel(self.frame,wx.Size(500,400))

        # add the panes to the manager
        #self._mgr.AddPane(menu, wx.LEFT, "")
        #self._mgr.AddPane(tb, wx.LEFT, "")
        self._mgr.AddPane(self.p1, wx.LEFT, "nav")
        self._mgr.AddPane(self.p2, wx.BOTTOM,"output")
        
        self._mgr.AddPane(p3, wx.CENTER,)

        all = self._mgr.GetAllPanes()
        
        #print self._mgr.LoadPerspective()
        self.str =  self._mgr.SavePerspective()
        #print self._mgr.GetFlags()
        #self._mgr.
        # 
        self._mgr.SetFlags(255)
        #print self._mgr.GetFlags()
        
        #print self._mgr.GetArtProvider()
        #self._mgr.SetArtProvider()
        
        #self._mgr.GetPane(self.p2).Hide()
        #self._mgr.Bind(event, handler, source, id, id2)
        """
        for one in all:
            one.Hide()
            one.Show()
            #print one.
        """

        # tell the manager to 'commit' all the changes just made
        self._mgr.Update()
        
        #wx.Log_SetActiveTarget(wx.LogStderr())
        #wx.Log_SetTraceMask(wx.TraceMessages)   
        return True
        
    def OnToolClick(self, event):
        #tb = self.GetToolBar()
        self.win.Begin()
        
        
    def OnSize(self, evt):
        size = self.win.GetSize()
        self.mysize = size
        """
        selfsize = self.win.GetSize()
        if selfsize[0] > 100:
            selfsize[0] = 100
        if selfsize[1] > 100:
            selfsize[1] = 100        
        
        
        self.myframe.SetSize(selfsize)
        """
        
        ### todo : -6 to constants class
        size = (size[0]-6, size[1])
        self.win.multiText.SetSize(size)
        

        evt.Skip()
        #if self.win.GetAutoLayout():
        #self.win.Layout()
        

        
    def OnCloseFrame(self, evt):
        self.frame.Close(True)
        #if hasattr(self, "window") and hasattr(self.window, "ShutdownDemo"):
        #self.window.ShutdownDemo()
        evt.Skip()
        


#
